---
id: Nginx的安装
sidebar_position: 2.3
description: Nginx可以选择编译安装和docker安装。
---
## Nginx的目录结构
```text
nginx
├── sbin
│   └── nginx   主程序
├── conf     配置文件目录
│   ├── nginx.conf 核心配置文件
│   └── mime.types 请求头，由服务器端告诉客户端发送的文件类型，浏览器会按照该类型解析该响应。
├── log
│   ├── access.log 访问日志
│   ├── error.log  访问出错日志
│   └── nginx.pid 记录 nginx 主进程id号
└── html 默认情况下的网页和站点资源
```
## Nginx 常用命令
```shell
# 启动 nginx
./sbin/nginx
# 停止nginx
./sbin/nginx -s stop
# 完成当前工作后停止
./sbin/nginx -s quit
# 在不停止nginx的情况下，重启nginx，重新加载配置文件，启动新的工作进程，完美停止旧的工作进程
./sbin/nginx -s reload
# 检测nginx配置文件是否可用
# nginx -t -c ./nginx.conf
```
## Nginx编译安装
### 1. 下载并上传解压到服务器
1. 下载地址 https://nginx.org/en/download.html。
2. 上传并解压
    ```shell
    tar -zxvf nginx-1.22.0.tar.gz
    ```
### 2. 编译安装和依赖检查
```shell title='安装依赖'
# 安装 c 语言编译器
yum install -y gcc
# 安装 perl 库
yum install -y pcre pcre-devel
# 安装 zlib 库
yum install -y zlib zlib-devel
```

```shell title='安装过程'
# --prefix=/usr/local/nginx 指定安装路径
# --with-http_ssl_module 要在 nginx 中配置 https，就必须安装ssl模块
[root@localhost nginx-1.22.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/user/local/nginx"
  nginx binary file: "/user/local/nginx/sbin/nginx"
  nginx modules path: "/user/local/nginx/modules"
  nginx configuration prefix: "/user/local/nginx/conf"
  nginx configuration file: "/user/local/nginx/conf/nginx.conf"
  nginx pid file: "/user/local/nginx/logs/nginx.pid"
  nginx error log file: "/user/local/nginx/logs/error.log"
  nginx http access log file: "/user/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

[root@localhost nginx-1.22.0]# make && make install

```
执行完成后，切换到 `/usr/local/` 目录下，就可以看到 `nginx` 文件夹，即nginx安装目录
```shell
[root@localhost nginx-1.22.0]# cd /usr/local/
[root@localhost local]# ll
总用量 0
drwxr-xr-x. 2 root  root    6 4月  11 2018 bin
drwxr-xr-x. 2 root  root    6 4月  11 2018 etc
drwxr-xr-x. 2 root  root    6 4月  11 2018 games
drwxr-xr-x. 2 root  root    6 4月  11 2018 include
drwxr-xr-x. 8 10143 10143 273 3月  10 19:27 jdk
drwxr-xr-x. 2 root  root    6 4月  11 2018 lib
drwxr-xr-x. 2 root  root    6 4月  11 2018 lib64
drwxr-xr-x. 2 root  root    6 4月  11 2018 libexec
drwxr-xr-x. 6 root  root   99 7月  16 23:31 maven
drwxr-xr-x. 6 root  root   54 8月   5 10:16 nginx
drwxr-xr-x. 2 root  root    6 4月  11 2018 sbin
drwxr-xr-x. 5 root  root   49 7月  16 15:32 share
drwxr-xr-x. 2 root  root    6 4月  11 2018 src
```
### 3. Nginx启停
进入 nginx sbin目录
```shell
# 启动
./nginx
# 快速停止
./nginx -s stop
# 优雅关停，在退出前完成已经接受的连接请求
./nginx -s quit
# 重新加载配置
./nginx -s reload
```
:::tip 关于防火墙

```shell
# 关闭防火墙
systemctl stop firewalld.service
# 禁止防火墙开机启动
systemctl disable firewalld.service
# 放行端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
# 重启防火墙
firewall-cmd --reload
```
:::
### 4. 安装成系统服务
1. 创建服务脚本
    ```shell
    vim /usr/lib/systemd/system/nginx.service
    ```
2. 服务脚本内容
    ```service
    [Unit]
    Description=nginx - web server
    After=network.target remote-fs.target nss-lookup.target

    [Service]
    Type=forking
    PIDFile=/usr/local/nginx/logs/nginx.pid
    ExecStartPre=/usr/local/nginx/sbin -t -c /usr/local/nginx/conf/nginx.conf
    ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    ExecReload=/usr/local/nginx/sbin/nginx -s reload
    ExecStop=/usr/local/nginx/sbin/nginx -s stop
    ExecQuit=/usr/local/nginx/sbin/nginx -s quit
    PrivateTmp=true

    [Install]
    wantedBy=multi-user.target
    ```
3. 重新加载系统服务
    ```shell
    systemctl daemon-reload
    ```
4. 启动服务
    ```shell
    systemctl start nginx
    ```

